package com.batis.exe;

import com.batis.config.Configuration;
import com.batis.config.MapperStatement;
import com.google.common.collect.Lists;

import java.sql.*;
import java.util.List;

/**
 * @author carter
 * create time    2018-08-17
 */
public class DefaultExecutor implements Executor {

    private Configuration configuration;

    public DefaultExecutor(Configuration configuration) {
        this.configuration = configuration;
    }

    @Override
    public <E> List<E> query(MapperStatement mapperStatement, Object param) {

        List<E> list = Lists.newArrayList();

        System.out.println("sql: "+mapperStatement.getSql());
        try (Connection connection = DriverManager.getConnection(configuration.getJdbcUrl(), configuration.getJdbcUsername(), configuration.getJdbcPassword());
             PreparedStatement statement = connection.prepareStatement(mapperStatement.getSql());
             ){

            setParam(statement,param);
            ResultSet resultSet = statement.executeQuery();

            doResult(resultSet,mapperStatement.getResultType(),list);

        } catch (SQLException e) {
            e.printStackTrace();
        } finally {


        }

        return list;
    }

    private <E> void doResult(ResultSet resultSet, String resultType, List<E> list) {

    }

    private void setParam(PreparedStatement statement, Object param) {

    }
}
